package siga.visao_financeiro;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;
import siga.visao_busca.buscaVer;
import siga.visao_busca.buscaDocFinVer;

public class movContasPagar extends javax.swing.JInternalFrame {
    Object[] JOopcoes = {"Sim","Não"};
    List CodObs = new ArrayList();
    List cod_matricula = new ArrayList();
    List cod_fase = new ArrayList();
    boolean busca_direta = false;

    /** Creates new form movContasPagar */
    public movContasPagar() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpPrincCMPagar = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtPagamento = new javax.swing.JTable();
        jpMenu = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jtfTipoBusca = new javax.swing.JComboBox();
        jToolBar1 = new javax.swing.JToolBar();
        jButton6 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        jbPagar = new javax.swing.JButton();
        jbEstornar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jbEditar = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Contas a Pagar");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/movimentacao_16x16.png"))); // NOI18N

        jpPrincCMPagar.setName("jpPrincCMPagar"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtPagamento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Pessoa/Fornecedor", "Histórico", "Dt. Venc.", "Valor Título", "Juros", "Dt. Pagto.", "Valor Pagto", "Situação"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtPagamento.setName("jtPagamento"); // NOI18N
        jtPagamento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtPagamentoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtPagamento);
        jtPagamento.getColumnModel().getColumn(0).setPreferredWidth(70);
        jtPagamento.getColumnModel().getColumn(0).setMaxWidth(70);
        jtPagamento.getColumnModel().getColumn(3).setPreferredWidth(80);
        jtPagamento.getColumnModel().getColumn(3).setMaxWidth(80);
        jtPagamento.getColumnModel().getColumn(4).setPreferredWidth(100);
        jtPagamento.getColumnModel().getColumn(4).setMaxWidth(100);
        jtPagamento.getColumnModel().getColumn(5).setPreferredWidth(100);
        jtPagamento.getColumnModel().getColumn(5).setMaxWidth(100);
        jtPagamento.getColumnModel().getColumn(6).setPreferredWidth(80);
        jtPagamento.getColumnModel().getColumn(6).setMaxWidth(80);
        jtPagamento.getColumnModel().getColumn(7).setPreferredWidth(100);
        jtPagamento.getColumnModel().getColumn(7).setMaxWidth(100);
        jtPagamento.getColumnModel().getColumn(8).setPreferredWidth(90);
        jtPagamento.getColumnModel().getColumn(8).setMaxWidth(90);

        jpMenu.setName("jpMenu"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo de Visão"));
        jPanel1.setName("jPanel1"); // NOI18N

        jtfTipoBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Pago", "A Pagar", "Estornado" }));
        jtfTipoBusca.setName("jtfTipoBusca"); // NOI18N
        jtfTipoBusca.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jtfTipoBuscaItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jtfTipoBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jtfTipoBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton6.setText("Busca");
        jButton6.setToolTipText("Busca por Pessoas");
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton6);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton2.setText("Acadêmico");
        jButton2.setToolTipText("Busca somente Acadêmicos");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton3.setText("Docente");
        jButton3.setToolTipText("Busca somente Docentes");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton4.setText("Fornecedor");
        jButton4.setToolTipText("Busca somente Fornecedores");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton5.setText("Funcionário");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton5);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_doc_32x32.png"))); // NOI18N
        jButton1.setText("Busca Documento");
        jButton1.setToolTipText("Busca por Documento");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);
        jToolBar2.setName("jToolBar2"); // NOI18N

        jbPagar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/baixar_32x32.png"))); // NOI18N
        jbPagar.setText("Pagar");
        jbPagar.setEnabled(false);
        jbPagar.setFocusable(false);
        jbPagar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbPagar.setName("jbPagar"); // NOI18N
        jbPagar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbPagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPagarActionPerformed(evt);
            }
        });
        jToolBar2.add(jbPagar);

        jbEstornar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/estornar_32x32.png"))); // NOI18N
        jbEstornar.setText("Estornar");
        jbEstornar.setEnabled(false);
        jbEstornar.setFocusable(false);
        jbEstornar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEstornar.setName("jbEstornar"); // NOI18N
        jbEstornar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbEstornar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEstornarActionPerformed(evt);
            }
        });
        jToolBar2.add(jbEstornar);

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/cxCancelar_32x32.png"))); // NOI18N
        jbCancelar.setText("Remover");
        jbCancelar.setEnabled(false);
        jbCancelar.setFocusable(false);
        jbCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jToolBar2.add(jbCancelar);

        jbEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/cxEditar_32x32.png"))); // NOI18N
        jbEditar.setText("Alterar");
        jbEditar.setEnabled(false);
        jbEditar.setFocusable(false);
        jbEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEditar.setName("jbEditar"); // NOI18N
        jbEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEditarActionPerformed(evt);
            }
        });
        jToolBar2.add(jbEditar);

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/gerar_men_32x32.png"))); // NOI18N
        jButton7.setText("Novo");
        jButton7.setToolTipText("Criar Menssalidades");
        jButton7.setFocusable(false);
        jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton7.setName("jButton7"); // NOI18N
        jButton7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton7);

        javax.swing.GroupLayout jpMenuLayout = new javax.swing.GroupLayout(jpMenu);
        jpMenu.setLayout(jpMenuLayout);
        jpMenuLayout.setHorizontalGroup(
            jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 975, Short.MAX_VALUE)
            .addGroup(jpMenuLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 873, Short.MAX_VALUE))
        );
        jpMenuLayout.setVerticalGroup(
            jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMenuLayout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jpPrincCMPagarLayout = new javax.swing.GroupLayout(jpPrincCMPagar);
        jpPrincCMPagar.setLayout(jpPrincCMPagarLayout);
        jpPrincCMPagarLayout.setHorizontalGroup(
            jpPrincCMPagarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 975, Short.MAX_VALUE)
            .addGroup(jpPrincCMPagarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jpMenu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpPrincCMPagarLayout.setVerticalGroup(
            jpPrincCMPagarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpPrincCMPagarLayout.createSequentialGroup()
                .addGap(116, 116, 116)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE))
            .addGroup(jpPrincCMPagarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpPrincCMPagarLayout.createSequentialGroup()
                    .addComponent(jpMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(413, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpPrincCMPagar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpPrincCMPagar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfTipoBuscaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jtfTipoBuscaItemStateChanged
        CarregaTabela();
}//GEN-LAST:event_jtfTipoBuscaItemStateChanged

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        buscaVer.setTipo("");
        OpenBuscaVer();
}//GEN-LAST:event_jButton6ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        buscaVer.setTipo("Academico");
        OpenBuscaVer();
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        buscaVer.setTipo("Docente");
        OpenBuscaVer();
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        buscaVer.setTipo("Fornecedor");
        OpenBuscaVer();
}//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        buscaVer.setTipo("Funcionario");
        OpenBuscaVer();
}//GEN-LAST:event_jButton5ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        buscaDocFinVer bdfv = new buscaDocFinVer(null,true);
        bdfv.setLocation((index.tela.width-bdfv.getWidth())/2, (index.tela.height-bdfv.getHeight())/2);
        bdfv.setVisible(true);
        if(buscaDocFinVer.isConfirmaBuscaDoc()){
            if(jbPagar.isEnabled())
                setBotoes();
            busca_direta=true;
            CarregaTabela();
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void jbPagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPagarActionPerformed
        busca_direta=true;
        PagarBoleto();
}//GEN-LAST:event_jbPagarActionPerformed

    private void jbEstornarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEstornarActionPerformed
        if(JOptionPane.showOptionDialog(null, "Deseja estornar o Pagamento de número: "+jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 0)+"?","Estorno",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,JOopcoes,JOopcoes[0])==JOptionPane.YES_OPTION){
            try {
                busca_direta=true;
                conexao.stm.executeUpdate("update fin_contas_mov_p set status=\"A Pagar\", cod_usuario="+index.usuario[0]+",cod_caixa=null,dt_pagamento=null,juros=null,desconto=null,valor=null " +
                        "where  codigo="+jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 0)+" and cod_emp="+index.cod_emp);
                conexao.stm.executeUpdate("update fin_caixa set banco_vl_atual=banco_vl_atual+"+index.ftes.parse(jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 7).toString()).doubleValue()+" where codigo=1 and cod_emp="+index.cod_emp);
                CarregaTabela();
            } catch (ParseException ex) {
                verErros.setErro(ex.getMessage());
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
       }else{
            JOptionPane.showMessageDialog(null, "Operação Cancelada.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
       }
}//GEN-LAST:event_jbEstornarActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        int[] x = jtPagamento.getSelectedRows();
        if (jtPagamento.getSelectedRowCount() < 1) {
            if (JOptionPane.showOptionDialog(null, "Deseja cancelar o pagamento de número: " + jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 0) + "?\nEsta ação implica em remover o registros financeiros do fornecador.", "Cancelamento", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, JOopcoes, JOopcoes[0]) == JOptionPane.YES_OPTION) {
                try {
                    busca_direta=true;
                    conexao.stm.executeUpdate("delete from fin_contas_mov_p where cod_emp=" + index.cod_emp + " and codigo=" + jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 0));
                    JOptionPane.showMessageDialog(null, "Cancelamento efetuado.", "Cancelamento", JOptionPane.INFORMATION_MESSAGE);
                } catch (SQLException ex) {
                    verErros.setErro(ex.getMessage());
                }
            }
        } else {
            String stBoleto="";
            for (int i = 0; i < jtPagamento.getSelectedRowCount(); i++) {
                stBoleto += jtPagamento.getValueAt(x[i], 0) + ",";
            }
            if (JOptionPane.showOptionDialog(null, "Deseja cancelar os pagamentos de número:\n" + stBoleto + "?\nEsta ação implica em remover o registros financeiros do fornecedor.", "Cancelamento", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, JOopcoes, JOopcoes[0]) == JOptionPane.YES_OPTION) {
                try {
                    busca_direta=true;
                    for (int i = 0; i < jtPagamento.getSelectedRowCount(); i++) {
                        conexao.stm.executeUpdate("delete from fin_contas_mov_p where cod_emp=" + index.cod_emp + " and codigo=" + jtPagamento.getValueAt(x[i], 0));
                    }
                    JOptionPane.showMessageDialog(null, "Cancelamento efetuado.", "Cancelamento", JOptionPane.INFORMATION_MESSAGE);
                } catch (SQLException ex) {
                    verErros.setErro(ex.getMessage());
                }
            }
        }
        CarregaTabela();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEditarActionPerformed
        java.awt.Dimension tela = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        movCaixaPagarEditarVer mcev = new movCaixaPagarEditarVer(null,true);
        mcev.setLocation((tela.width-mcev.getWidth())/2, (tela.height-mcev.getHeight())/2);
        mcev.cod_lanc = jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 0).toString();
        mcev.setVisible(true);
        if(mcev.finaliza){
            setBotoes();
            busca_direta=true;
            CarregaTabela();
        }

}//GEN-LAST:event_jbEditarActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        movContasPagarNovo mcg = new movContasPagarNovo(null,true);
        mcg.setLocation((index.tela.width-mcg.getWidth())/2, (index.tela.height-mcg.getHeight())/2);
        mcg.setVisible(true);
        if(mcg.finaliza){
            CarregaTabela();
        }
}//GEN-LAST:event_jButton7ActionPerformed

    private void jtPagamentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtPagamentoMouseClicked
        if(evt.getClickCount()==2){
            PagarBoleto();
        }else if(!jbEstornar.isEnabled()){
            setBotoes();
        }
    }//GEN-LAST:event_jtPagamentoMouseClicked

    protected void OpenBuscaVer(){
        buscaVer bv = new buscaVer(null,true);
        bv.setLocation((index.tela.width-bv.getWidth())/2, (index.tela.height-bv.getHeight())/2);
        bv.setVisible(true);
        if(buscaVer.isConfirmaBusca()){
            if(jbPagar.isEnabled())
                setBotoes();
            CarregaTabela();
        }
    }

    public void PagarBoleto(){
        if(jtPagamento.getSelectedRowCount() > 0){
            if(jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 8).equals("A Pagar")||jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 8).equals("Vencida")||jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 8).equals("Parcial")){
                movCaixaPagarVer mcbv = new movCaixaPagarVer(null,true);
                mcbv.setLocation((index.tela.width-mcbv.getWidth())/2, (index.tela.height-mcbv.getHeight())/2);
                mcbv.Codigo = jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 0).toString();
                mcbv.Nome = jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 1).toString();
                mcbv.DtVenc = jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 3).toString();
                mcbv.Valor = jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 4).toString();
                mcbv.setVisible(true);
                if(movCaixaPagarVer.isConfirmaPagar()){
                    setBotoes();
                    CarregaTabela();
                }
            }else{
                JOptionPane.showMessageDialog(null, "Não é possível pagar.\n Situação:"+jtPagamento.getValueAt(jtPagamento.getSelectedRow(), 8),"Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
            }
        }else{
          JOptionPane.showMessageDialog(null, "Selecione uma linha.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void CarregaTabela(){
        if(!index.LRpessoa.get(0).toString().equals("")||busca_direta==true){
            try {
                String vtipobusca;
                String vpessoa;
                String vnrdoc;
                String vdtvenc;
                String vdtpag;
                String vvlbr;
                String vvlrec;
                String vdescricao;
                String vdtvencperiodo;
                String vdtpgtoperiodo;
                String dtTabela1;
                String dtTabela2;
                busca_direta=false;
                javax.swing.table.DefaultTableModel jtb = (javax.swing.table.DefaultTableModel) jtPagamento.getModel();
                for (int x = jtPagamento.getRowCount() - 1; x >= 0; --x) {
                    jtb.removeRow(x);
                }
                vtipobusca = (jtfTipoBusca.getSelectedItem().equals("Todos")) ? "" : " and fr.status=\"" + jtfTipoBusca.getSelectedItem() + "\"";
                vpessoa = (index.LRpessoa.get(0).toString().equals("")) ? "" : " and fr.cod_pessoa=" + index.LRpessoa.get(0).toString();
                vnrdoc = (index.pesquisa.get(0).equals("")) ? "" : " and fr.codigo=" + index.pesquisa.get(0);
                vdtvenc = (index.pesquisa.get(1).equals("  /  /    ")) ? "" : new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd/MM/yyyy").parse(index.pesquisa.get(1).toString()));
                vdtvenc = (vdtvenc.equals("")) ? "" : " and fr.dt_vencimento like \"" + vdtvenc + "%\"";
                vdtpag = (index.pesquisa.get(2).equals("  /  /    ")) ? "" : new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd/MM/yyyy").parse(index.pesquisa.get(2).toString()));
                vdtpag = (vdtpag.equals("")) ? "" : " and fr.dt_pagamento like \"" + vdtpag + "%\"";
                vvlbr = (index.pesquisa.get(3).equals("")) ? "" : " and fr.valor_bruto=\"" +index.ftes.parse(index.pesquisa.get(3).toString()).doubleValue()+ "\"";
                vvlrec = (index.pesquisa.get(4).equals("")) ? "" : " and fr.valor=\"" + index.ftes.parse(index.pesquisa.get(4).toString()).doubleValue() + "\"";
                vdescricao = (index.pesquisa.get(5).equals("")) ? "" : " and fr.descricao like \"%"+index.pesquisa.get(5)+"%\"";
                vdtpgtoperiodo = (index.pesquisa.get(8).equals("  /  /    ")||index.pesquisa.get(9).equals("  /  /    "))?"":" and (fr.dt_pagamento BETWEEN \""+index.dtcv.converte("US", index.pesquisa.get(8).toString())+" 00:00:00\" and \""+index.dtcv.converte("US", index.pesquisa.get(9).toString())+" 23:59:59\") ";
                vdtvencperiodo = (index.pesquisa.get(6).equals("  /  /    ")||index.pesquisa.get(7).equals("  /  /    "))?"":" and (fr.dt_vencimento BETWEEN \""+index.dtcv.converte("US", index.pesquisa.get(6).toString())+" 00:00:00\" and \""+index.dtcv.converte("US", index.pesquisa.get(7).toString())+" 23:59:59\") ";
                conexao.Query("select fr.codigo, fr.cod_pessoa, p.descricao as desc_pessoa, fr.descricao, fr.dt_pagamento,fr.dt_vencimento,fr.valor_bruto,fr.juros,fr.valor,fr.status "
                        + " from fin_contas_mov_p fr inner join pessoas p on p.codigo=fr.cod_pessoa and p.cod_emp=fr.cod_emp "
                        + " where fr.cod_emp=" + index.cod_emp + " " + vdescricao + " " + vpessoa + " " + vdtpgtoperiodo + " " + vdtvencperiodo + " " + vtipobusca + " " + vnrdoc + " " + vdtvenc + " " + vdtpag + " " + vvlbr + " " + vvlrec + " " + " order by fr.dt_pagamento,fr.dt_vencimento DESC");
                while (conexao.rs.next()) {
                    if (conexao.rs.getString("dt_vencimento") != null) {
                        dtTabela1 = new SimpleDateFormat("dd/MM/yyyy").format(conexao.rs.getDate("dt_vencimento"));
                    } else {
                        dtTabela1 = "";
                    }
                    if (conexao.rs.getString("dt_pagamento") != null) {
                        dtTabela2 = new SimpleDateFormat("dd/MM/yyyy").format(conexao.rs.getDate("dt_pagamento"));
                    } else {
                        dtTabela2 = "";
                    }
                    jtb.addRow(new Object[]{conexao.rs.getString("codigo"), conexao.rs.getString("desc_pessoa"), conexao.rs.getString("descricao"), dtTabela1, index.ftbr.format(conexao.rs.getDouble("valor_bruto")), index.ftbr.format(conexao.rs.getDouble("juros")),dtTabela2, index.ftbr.format(conexao.rs.getDouble("valor")),conexao.rs.getString("status")});
                }
            } catch (ParseException ex) {
                verErros.setErro(ex.getMessage());
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            } 
        }
    }
  
    private void setBotoes(){
            jbPagar.setEnabled(!jbPagar.isEnabled());
            jbCancelar.setEnabled(!jbCancelar.isEnabled());
            jbEstornar.setEnabled(!jbEstornar.isEnabled());
            jbEditar.setEnabled(!jbEditar.isEnabled());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbEstornar;
    private javax.swing.JButton jbPagar;
    private javax.swing.JPanel jpMenu;
    private javax.swing.JPanel jpPrincCMPagar;
    private javax.swing.JTable jtPagamento;
    private javax.swing.JComboBox jtfTipoBusca;
    // End of variables declaration//GEN-END:variables

}
